﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using System.Xml;
using YArchitech.LIB;
using HYCodeBase.WinForm.WinFormRemindSuit;
using HYCodeBase.XmlUtility;

namespace HYRevitCode.FormUtility
{
	public class ControlInfoManager
	{
		public string StrFileName
		{
			get
			{
				return this.m_strFileName;
			}
			set
			{
				this.m_strFileName = value;
			}
		}

		public ControlInfoManager(System.Windows.Forms.Form inputForm, string inputKeyString = null, string inputSavePath = null)
		{
			this.m_thisForm = inputForm;
			this.m_strKeystring = inputKeyString;
			this.m_thisManager = new WinFormRemindManger(inputForm, this.m_strKeystring);
			if (inputSavePath != null)
			{
				this.m_strUseDataPath = inputSavePath;
			}
		}

		public ControlInfoManager(System.Windows.Forms.Form inputForm, List<KeyValuePair<System.Windows.Forms.Control, bool>> inputControlGroup, string inputKeyString = null, string inputSavePath = null)
		{
			this.m_thisForm = inputForm;
			this.m_lstControlGroup = (from n in inputControlGroup
			select new KeyValuePair<ControlPacker, bool>(new ControlPacker
			{
				UseControl = n.Key
			}, n.Value)).ToList<KeyValuePair<ControlPacker, bool>>();
			this.m_strKeystring = inputKeyString;
			this.m_thisManager = new WinFormRemindManger(inputForm, this.m_lstControlGroup, this.m_strKeystring);
			if (inputSavePath != null)
			{
				this.m_strUseDataPath = inputSavePath;
			}
		}

		public ControlInfoManager(System.Windows.Forms.Form inputForm, List<KeyValuePair<ControlPacker, bool>> inputControlGroup, string inputKeyString = null, string inputSavePath = null)
		{
			this.m_thisForm = inputForm;
			this.m_lstControlGroup = inputControlGroup;
			this.m_strKeystring = inputKeyString;
			this.m_thisManager = new WinFormRemindManger(inputForm, this.m_lstControlGroup, this.m_strKeystring);
			if (inputSavePath != null)
			{
				this.m_strUseDataPath = inputSavePath;
			}
		}

		public void AddControl(System.Windows.Forms.Control inputControl, bool ifIsSpecialValue = false)
		{
			if (inputControl == null)
			{
				return;
			}
			this.m_lstControlGroup.Add(new KeyValuePair<ControlPacker, bool>(new ControlPacker
			{
				UseControl = inputControl
			}, ifIsSpecialValue));
		}

		public void AddControl(ControlPacker inputControl, bool ifIsSpecialValue = false)
		{
			if (inputControl == null)
			{
				return;
			}
			this.m_lstControlGroup.Add(new KeyValuePair<ControlPacker, bool>(inputControl, ifIsSpecialValue));
		}

		public bool GetControlInfoToXml()
		{
			XmlNode xmlNode = null;
			bool flag = false;
			string text = this.m_strUseDataPath + this.StrFileName;
			try
			{
				XmlElement xmlRootNode = new HYCodeBase.XmlUtility.XmlHelper(text, this.m_strNodeNameOfRoot).XmlRootNode;
				XmlDocument ownerDocument = xmlRootNode.OwnerDocument;
				if (!this.m_thisManager.TrySaveFormToXmlNode(ref ownerDocument, out xmlNode))
				{
					return false;
				}
				for (int i = 0; i < xmlRootNode.ChildNodes.Count; i++)
				{
					XmlAttributeCollection attributes = xmlRootNode.ChildNodes[i].Attributes;
					if (attributes.Count == xmlNode.Attributes.Count)
					{
						for (int j = 0; j < xmlNode.Attributes.Count; j++)
						{
							if (attributes[j].Name.Equals(xmlNode.Attributes[j].Name))
							{
								if (attributes[j].Name.Equals(this.m_strAttributeName) && !attributes[j].Value.Equals(xmlNode.Attributes[j].Value))
								{
									break;
								}
								if (xmlRootNode.ChildNodes[i].InnerText.Equals(xmlNode.InnerText))
								{
									flag = true;
									xmlRootNode.ReplaceChild(xmlNode, xmlRootNode.ChildNodes[i]);
									break;
								}
							}
						}
					}
				}
				if (!flag)
				{
					xmlRootNode.AppendChild(xmlNode);
				}
				ownerDocument.Save(text);
			}
			catch (Exception)
			{
				return false;
			}
			return true;
		}

		public bool SetControlInfoToForm()
		{
			List<System.Windows.Forms.Control> list;
			return this.SetControlInfoToForm(out list);
		}

		public bool SetControlInfoToForm(out List<System.Windows.Forms.Control> lstNonSavedControl)
		{
			string text = this.m_strUseDataPath + this.StrFileName;
			lstNonSavedControl = new List<System.Windows.Forms.Control>();
			List<System.Windows.Forms.Control> list = new List<System.Windows.Forms.Control>();
			list.Add(this.m_thisForm);
			foreach (KeyValuePair<ControlPacker, bool> keyValuePair in this.m_lstControlGroup)
			{
				if (keyValuePair.Key != null)
				{
					list.Add(keyValuePair.Key.UseControl);
				}
			}
			if (!File.Exists(text))
			{
				lstNonSavedControl = list;
				return false;
			}
			bool result;
			try
			{
				XmlElement xmlRootNode = new HYCodeBase.XmlUtility.XmlHelper(text, this.m_strNodeNameOfRoot).XmlRootNode;
				if (xmlRootNode == null)
				{
					lstNonSavedControl = list;
					result = false;
				}
				else
				{
					result = this.m_thisManager.TryRendXmlToForm(xmlRootNode, out lstNonSavedControl);
				}
			}
			catch (Exception)
			{
				lstNonSavedControl = list;
				result = false;
			}
			return result;
		}

		private System.Windows.Forms.Form m_thisForm;

		private List<KeyValuePair<ControlPacker, bool>> m_lstControlGroup = new List<KeyValuePair<ControlPacker, bool>>();

		private WinFormRemindManger m_thisManager;

		private string m_strFileName = "\\ControlDataInfo.xml";

		private string m_strNodeNameOfRoot = "Root";

		private string m_strAttributeName = "Name";

		private string m_strUseDataPath = Product.UserDataLocation;

		private string m_strKeystring;
	}
}
